<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>5-7 元素居中：calc</title>
  <style>
    .parent {
      width: 200px;
      height: 200px;
      background: #ccc;
      /* 父相子绝 参考 */
      position: relative;
    }

    .child {
      width: 100px;
      height: 100px;
      background: #e00;
      /* calc太好用了 一定要记得是，calc运算符两侧必须有空格，不然无效 */
      position: absolute;
      left: calc(50% - 50px);
      top: calc(50% - 50px);
      /* 执行上面这两段代码，就相当于执行了下面这些 */
      /* left: 50%;
      top: 50%;
      margin-left: -100px;
      margin-top: -100px; */
    }
  </style>

</head>

<body>
  <div class="parent">
    <div class="child"></div>
  </div>
</body>

</html>